/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.schedul.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.schedul.entity.SchedulingInformation;
import com.thinkgem.jeesite.modules.schedul.dao.SchedulingInformationDao;
import com.thinkgem.jeesite.modules.shift.entity.Shift;
import com.thinkgem.jeesite.modules.shift.service.ShiftService;

/**
 * 员工排班表Service
 * @author pan.zhou
 * @version 2017-07-15
 */
@Service
@Transactional(readOnly = true)
public class SchedulingInformationService extends CrudService<SchedulingInformationDao, SchedulingInformation> {

	@Autowired
	private ShiftService shiftService;
	
	@Autowired
	private SchedulingInformationDao dao;
	
	public SchedulingInformation get(String id) {
		return super.get(id);
	}
	
	public List<SchedulingInformation> findList(SchedulingInformation schedulingInformation) {
		return super.findList(schedulingInformation);
	}
	
	public Page<SchedulingInformation> findPage(Page<SchedulingInformation> page, SchedulingInformation schedulingInformation) {
		return super.findPage(page, schedulingInformation);
	}
	
	@Transactional(readOnly = false)
	public void save(SchedulingInformation schedulingInformation) {
		super.save(schedulingInformation);
	}
	
	@Transactional(readOnly = false)
	public void update(SchedulingInformation schedulingInformation) {
		//先修改原来的数据状态及调整班次结束时间
		updateData(schedulingInformation);
		//修改时重新新增一条记录
		schedulingInformation.setId(null);
		super.save(schedulingInformation);
	}
	
	@Transactional(readOnly = false)
	public void updateData(SchedulingInformation schedulingInformation) {
		dao.updateData(schedulingInformation);
	}
	
	@Transactional(readOnly = false)
	public void delete(SchedulingInformation schedulingInformation) {
		super.delete(schedulingInformation);
	}
	/**
	 * 
	 * @param schclassId 单个考勤时段ID
	 * @param employeeId 多个员工ID
	 */
	@Transactional(readOnly = false)
	public void saveData(String schclassId,String employeeNumber) {
		SchedulingInformation info = null;
		
		//根据ID查询班次信息
		Shift shift = shiftService.get(schclassId);
		
		//查找是否存在“，”
		int count = employeeNumber.indexOf(",");
		if(-1 != count)
		{
			String[] array = employeeNumber.split(",");
			for (int i = 0; i < array.length; i++) {
				String number = array[i];
				info = new SchedulingInformation();		
				info.setEmployeeNumber(number);// 员工编号
				info.setShift(shift);//班次
				info.setStarttime(shift.getStartdate());// 开始日期
				info.setEndtime(shift.getEnddate());// 结束日期
				info.setCompany(shift.getCompany());// 归属公司 ,国家
				info.setOffice(shift.getOffice()); // 归属部门,仓库    
				super.save(info);
			}//end for
			
		}else{
			info = new SchedulingInformation();		
			info.setEmployeeNumber(employeeNumber);// 员工编号
			info.setShift(shift);//班次
			info.setStarttime(shift.getStartdate());// 开始日期
			info.setEndtime(shift.getEnddate());// 结束日期
			info.setCompany(shift.getCompany());// 归属公司 ,国家
			info.setOffice(shift.getOffice()); // 归属部门,仓库    
			super.save(info);
		}
	}
}